package com.zzyl.nursing.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.constant.DevicesAndProduct;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.Product;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author ruoyi
 * @date 2025-07-20
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 设备注册
     * @return
     */
    @ApiOperation(value = "设备注册")
    @PostMapping("/register")
    public AjaxResult register(@ApiParam(value = "设备信息")@RequestBody DeviceDto deviceDto){
        deviceService.register(deviceDto);
        return AjaxResult.success();
    }

    /**
     * 查询设备列详情
     * @param iotId
     * @return
     */
    @GetMapping("/{iotId}")
    public AjaxResult queryDeviceDetail(@PathVariable String iotId){
        return AjaxResult.success(deviceService.queryDeviceDetail(iotId));
    }


    /**
     * 同步产品列表
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "同步产品列表")
    public AjaxResult syncProductList(){
        deviceService.syncProductList();
        return AjaxResult.success();
    }

    /**
     * 获取所有产品
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "获取所有产品")
    public AjaxResult allProduct(){
        String s = redisTemplate.opsForValue().get(DevicesAndProduct.DEVICE_TYPE_PRODUCT);
        if (s != null) {
            JSONArray objects = JSONUtil.parseArray(s);
            List<Product> products = objects.toList(Product.class);
            List<ProductVo> productVos = new ArrayList<>();
            for (Product product : products) {
                productVos.add(new ProductVo(product.getProductId(), product.getName()));
            }
            return AjaxResult.success(productVos);
        }
        return AjaxResult.success();
    }

    /**
     * 删除设备
     * @param iotId
     * @return
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation(value = "删除设备")
    public AjaxResult delete(@ApiParam (value = "设备ID") @PathVariable String iotId){
        deviceService.deleteDeviceByIotId(iotId);
        return AjaxResult.success();
    }


    /**
     * 查询设备物模型属性
     * @return
     */
    @GetMapping("/queryServiceProperties/{nodeId}")
    @ApiOperation(value = "查询设备物模型属性")
    public AjaxResult queryServicePropert(@ApiParam (value = "设备标识码")@PathVariable String nodeId){
        return AjaxResult.success(deviceService.queryServiceProperties(nodeId));
    }

    /**
     * 查询产品服务
     * @param productKey
     * @return
     */
    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品服务")
    public AjaxResult queryProduct(@ApiParam (value = "产品标识码")@PathVariable String productKey){
        return AjaxResult.success(deviceService.queryProduct(productKey));
    }

}
